package com.authine.cloudpivot.ext.utils.system;

import com.alibaba.cola.exception.BizException;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ServiceUrlConst;
import com.authine.cloudpivot.ext.dto.system.DepartmentDto;
import com.authine.cloudpivot.ext.utils.HttpClients;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @ClassName DeptUtils
 * @Description: 部门工具类，方便调用
 * @Author fj
 * @Date 2021/8/20
 * @Version V1.0
 **/
public class DeptUtils {
    /**
     * 根据部门id获取部门信息
     *
     * @param deptId 部门id
     * @return 部门信息
     */
    public static DepartmentDto getDeptByDeptId(String deptId) {
        String url = HttpClients.getUrl(AppConst.SYSTEM_MANAGEMENT, ServiceUrlConst.GET_DEPT_BY_ID_THROUGH_JDBC_URL);
        Map<String, Object> deptMap = Maps.newHashMap();
        deptMap.put("id", deptId);
        return HttpClients.execute(url, deptMap, DepartmentDto.class);
    }

    /**
     * 通过部门id判断当前部门是否存在
     *
     * @param deptId 部门id
     * @return true：存在 false：不存在
     */
    public static boolean exists(String deptId) {
        boolean result;
        try {
            // 部门id为空会抛出异常
            getDeptByDeptId(deptId);
            result = true;
        } catch (BizException e) {
            result = false;
        }
        return result;
    }
}
